package com.doordu.soa.service.data.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.doordu.soa.service.comm.utils.BeanCopyUtil;
import com.doordu.soa.service.comm.utils.DateUtils;
import com.doordu.soa.service.data.dao.LogSyncErrorMapper;
import com.doordu.soa.service.data.dao.LogSyncMapper;
import com.doordu.soa.service.data.entity.LogSync;
import com.doordu.soa.service.data.entity.LogSyncError;
import com.doordu.soa.service.data.entity.example.LogSyncErrorExample;
import com.doordu.soa.service.data.entity.example.LogSyncExample;
import com.doordu.soa.service.data.service.LogSyncService;
import com.doordu.soa.service.data.vo.LogSyncErrorVo;
import com.doordu.soa.service.data.vo.LogSyncResultVo;
import com.doordu.soa.service.data.vo.LogSyncVo;
import com.doordu.soa.service.data.vo.QuerySyncLogVo;

/**
 * 同步日志服务[实现类]
 * 
 * @ClassName: LogSyncServiceImpl
 * @Description:
 * @author xiaoqun.yi yixq@doordu.com
 * @date 2017年9月5日 上午10:52:21
 * @version V1.0
 */
@Service
public class LogSyncServiceImpl implements LogSyncService {
	private final Logger logger = LoggerFactory.getLogger(LogSyncServiceImpl.class);

	@Value("${file.parentPath}")
	private String parentPath;

	@Autowired
	private LogSyncMapper logSyncMapper;

	@Autowired
	private LogSyncErrorMapper logSyncErrorMapper;

	@Override
	public int insert(LogSyncVo logSyncVo) {
		LogSync entity = new LogSync();
		BeanUtils.copyProperties(logSyncVo, entity);
		return logSyncMapper.insertSelective(entity);
	}

	@Override
	public LogSyncVo insertLogSync(Integer companyId, Integer parkingId, String serviceMethod, String serviceName,
			Integer successCount, Integer failCount, Short operateType, Short status, Short gzipFlag, String dataFile,
			String dataJson) {
		LogSync entity = new LogSync();
		entity.setCompanyId(companyId);
		entity.setParkingId(parkingId);
		entity.setServiceMethod(serviceMethod);
		entity.setServiceName(serviceName);
		entity.setSuccessCount(successCount);
		entity.setFailCount(failCount);
		entity.setOperateType(operateType);
		entity.setStatus(status);
		entity.setGzipFlag(gzipFlag);
		entity.setDataFile(dataFile);
		entity.setDataJson(dataJson);
		entity.setCreateTime(new Date());
		logSyncMapper.insertSelective(entity);
		LogSyncVo logSyncVo = new LogSyncVo();
		BeanUtils.copyProperties(entity, logSyncVo);
		logger.error("******log_id=" + entity.getLogId());
		return logSyncVo;
	}

	@Override
	public int update(LogSyncVo logSyncVo) {
		LogSync entity = new LogSync();
		BeanUtils.copyProperties(logSyncVo, entity);
		return logSyncMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public String getParentPath() {
		return parentPath;
	}

	@Override
	public LogSyncErrorVo insertLogSyncError(Long logId, String errorJsondata, String errorReason, Short errorType) {
		LogSyncError entity = new LogSyncError();
		entity.setErrorJsondata(errorJsondata);
		entity.setLogId(logId);
		entity.setErrorType(errorType);
		entity.setErrorReason(errorReason);
		logSyncErrorMapper.insertSelective(entity);
		LogSyncErrorVo vo = new LogSyncErrorVo();
		BeanUtils.copyProperties(entity, vo);
		return vo;
	}

	@Override
	public List<LogSyncResultVo> queryLogSync(QuerySyncLogVo query) {
		LogSyncExample example = new LogSyncExample();
		LogSyncExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(query.getCompanyId());
		criteria.andServiceMethodEqualTo(query.getServiceMethod());
		criteria.andCreateTimeBetween(DateUtils.dateStrToDate(DateUtils.DATE_TIME_FMT, query.getBeginTime() + ":00"),
				DateUtils.dateStrToDate(DateUtils.DATE_TIME_FMT, query.getEndTime() + ":00"));
		List<LogSync> list = logSyncMapper.selectByExample(example);
		if (null != list && list.size() > 0) {
			return BeanCopyUtil.copyList(list, LogSyncResultVo.class);
		}
		return null;
	}

	@Override
	public List<LogSyncErrorVo> queryLogSyncError(Long logId) {
		LogSyncErrorExample example = new LogSyncErrorExample();
		LogSyncErrorExample.Criteria criteria = example.createCriteria();
		criteria.andLogIdEqualTo(logId);
		List<LogSyncError> list = logSyncErrorMapper.selectByExample(example);
		if (null != list && list.size() > 0) {
			return BeanCopyUtil.copyList(list, LogSyncErrorVo.class);
		}
		return null;
	}

}
